package com.xbai.user.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.user.pojo.Address;
import com.xbai.user.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc AddressController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/address")
@CrossOrigin
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    /***
     * Address分页条件搜索实现
     * @param address {@link Address}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Address address, @PathVariable  int page, @PathVariable  int size){
        //调用AddressService实现分页条件查询Address
        PageInfo<Address> pageInfo = addressService.findPage(address, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Address分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AddressService实现分页查询Address
        PageInfo<Address> pageInfo = addressService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Address数据
     * @param address {@link Address}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Address>> findList(@RequestBody(required = false) Address address){
        //调用AddressService实现条件查询Address
        List<Address> list = addressService.findList(address);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Address数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用AddressService实现根据主键删除
        addressService.delete(id);
        return new Result();
    }

    /***
     * 修改Address数据
     * @param address {@link Address}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Address address, @PathVariable Integer id){
        //设置主键值
        address.setId(id);
        //调用AddressService实现修改Address
        addressService.update(address);
        return new Result();
    }

    /***
     * 新增Address数据
     * @param address {@link Address}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Address address){
        //调用AddressService实现添加Address
        addressService.add(address);
        return new Result();
    }

    /***
     * 根据ID查询Address数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<Address> findById(@PathVariable Integer id){
        //调用AddressService实现根据主键查询Address
        Address address = addressService.findById(id);
        return new Result<>(address);
    }

    /***
     * 查询Address全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Address>> findAll(){
        //调用AddressService实现查询所有Address
        List<Address> list = addressService.findAll();
        return new Result<>(list) ;
    }
}
